package com.mvc.rest;  
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.validation.Valid;

import net.sf.json.JSONObject;

import org.liyufeng.project.addresslist.DepartmentManagement;
import org.liyufeng.project.pojo.Token;
import org.liyufeng.project.pojo.WeixinDepartment;
import org.liyufeng.project.util.CommonUtil;
import org.liyufeng.project.util.ParamesAPI;
import org.springframework.stereotype.Controller;  
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;  
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.servlet.ModelAndView;  

import com.mvc.model.Compay;
@Controller  

public class RestConstroller {  
	private DepartmentManagement departmentManagement;
	private CommonUtil util;
	// 凭证获取（GET）
		public final static String token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CORPID&corpsecret=CORPSECRET";
    public RestConstroller() {
    	
    } 
    
    @RequestMapping(value = "/queryByid", method = RequestMethod.GET)//使用post方法的时候  
    public String queryById(HttpServletRequest req,HttpServletResponse response){  
    	 String id =req.getParameter("id");
    	 WeixinDepartment dep=new WeixinDepartment();
    	 Token token = CommonUtil.getToken(ParamesAPI.corpId, ParamesAPI.corpsecret);
     	List<WeixinDepartment> departmentList=departmentManagement.GetListByID(token.getAccessToken(),id);
     	for (WeixinDepartment weixinDepartment : departmentList) {
			if(id.equals(String.valueOf(weixinDepartment.getId()))){
				dep.setId(weixinDepartment.getId());
				dep.setName(weixinDepartment.getName());
				dep.setParentid(weixinDepartment.getParentid());
			}
		}
     	List<WeixinDepartment> list=new ArrayList<WeixinDepartment>();
     	if(dep!=null){
     		list.add(dep);
         	req.setAttribute("list", list);
     	}
    	return "user";
    }
    @RequestMapping(value = "/welcome", method = RequestMethod.GET)  
    public String registPost(HttpServletRequest request,HttpServletResponse response) throws Exception  { 
    	Token token = CommonUtil.getToken(ParamesAPI.corpId, ParamesAPI.corpsecret);
    	List<WeixinDepartment> departmentList=departmentManagement.GetList(token.getAccessToken());
    	request.setAttribute("list", departmentList);
    	return "user";
    }  

    @RequestMapping(value = "/add", method = RequestMethod.GET)  
    public String addPost(HttpServletRequest request,HttpServletResponse response) throws Exception  { 
    	String id=request.getParameter("id");
    	WeixinDepartment dep=new WeixinDepartment();
    	if(id!=null){
    		 Token token = CommonUtil.getToken(ParamesAPI.corpId, ParamesAPI.corpsecret);
    	     List<WeixinDepartment> departmentList=departmentManagement.GetListByID(token.getAccessToken(), id);
    	     for (WeixinDepartment weixinDepartment : departmentList) {
				if(id.equals(String.valueOf(weixinDepartment.getId()))){
					dep.setId(weixinDepartment.getId());
					dep.setName(weixinDepartment.getName());
				}
			}
    	     request.setAttribute("departmentList", dep);
    	     return "updateCompany";
    	     
    	}else{
    		 return "addCompany";
    	}
    	
    	
    }
    @RequestMapping(value = "/save", method = RequestMethod.GET)//使用post方法的时候  
    public String add(@Valid Compay compay,BindingResult binding,Model model,HttpServletRequest req){  
    	String access_token = token_url.replace("CORPID", ParamesAPI.corpId).replace("CORPSECRET", ParamesAPI.corpsecret);
		// 发起GET请求获取凭证
		JSONObject jsonObject = util.httpsRequest(access_token, "GET", null);
    	String name =req.getParameter("name");
    	departmentManagement.Create(jsonObject.getString("access_token"), name, "1");
    
        return "redirect:/welcome";  
    }
    
    @RequestMapping(value = "/update", method = RequestMethod.GET)//使用post方法的时候  
    public String update(@Valid Compay compay,BindingResult binding,Model model,HttpServletRequest req){  
    	System.out.println("修改。。。。");
    	String access_token = token_url.replace("CORPID", ParamesAPI.corpId).replace("CORPSECRET", ParamesAPI.corpsecret);
		// 发起GET请求获取凭证
		JSONObject jsonObject = util.httpsRequest(access_token, "GET", null);
		
    	String name =req.getParameter("name");
    	String id =req.getParameter("id");
    	try {
			String companyName=new String(name.getBytes("ISO-8859-1"),"UTF-8");
			departmentManagement.Update(jsonObject.getString("access_token"), id, companyName);
	        
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    	 return "redirect:/welcome"; 
    }
    
    @RequestMapping(value = "/del", method = RequestMethod.GET)  
    public String delPost(HttpServletRequest request,HttpServletResponse response) throws Exception  {
    	System.out.println("删除。。。。。");
    	String access_token = token_url.replace("CORPID", ParamesAPI.corpId).replace("CORPSECRET", ParamesAPI.corpsecret);
		// 发起GET请求获取凭证
		JSONObject jsonObject = util.httpsRequest(access_token, "GET", null);
		
    	String id =request.getParameter("id");
    	departmentManagement.Delete(jsonObject.getString("access_token"), id);
		return "redirect:/welcome";
    	
    }

	public DepartmentManagement getDepartmentManagement() {
		return departmentManagement;
	}

	public void setDepartmentManagement(DepartmentManagement departmentManagement) {
		this.departmentManagement = departmentManagement;
	}

	public void setUtil(CommonUtil util) {
		this.util = util;
	}  
    
}  